package com.anyisn.anyflickr.shell.view
{
	import flash.events.Event;
	
	import org.puremvc.as3.multicore.interfaces.IMediator;
	import org.puremvc.as3.multicore.interfaces.INotification;
	import org.puremvc.as3.multicore.patterns.mediator.Mediator;
	
	import com.anyisn.anyflickr.shell.ShellFacade;
	import com.anyisn.anyflickr.shell.view.components.PreviousAndNextHB;
	import com.anyisn.anyflickr.shell.view.components.SlideCB;

	public class SlideCBMediator extends Mediator implements IMediator
	{
		public static const NAME:String = 'SlideCBMediator';
		
		public function SlideCBMediator(viewComponent:Object)
		{
			super(NAME, viewComponent);
			
			cb.addEventListener(SlideCB.PHOTO_CHANGE, onPhotoChange);
			cb.addEventListener(SlideCB.PAUSE, onPause);
			cb.addEventListener(SlideCB.CONTINUE, onContinue);
		}
		
		override public function listNotificationInterests():Array {
			return [ShellFacade.STARTED,
					ShellFacade.PAUSED,
					ShellFacade.CONTINUED,
					ShellFacade.ENDED,
					ShellFacade.PHOTO_LOAD_COMPLETE
					];
		}
		
		override public function handleNotification(notification:INotification):void {
			switch(notification.getName()) {
				case ShellFacade.STARTED:
					var length:uint = notification.getBody() as uint;
					slidePN.max = length;
					cb.currentState = SlideCB.STATE_SLIDING;
					break;
				case ShellFacade.PAUSED:
					cb.currentState = SlideCB.STATE_PAUSED;
					break;
				case ShellFacade.CONTINUED:
					cb.currentState = SlideCB.STATE_SLIDING;
					break;
				case ShellFacade.ENDED:
					cb.currentState = SlideCB.STATE_ENDED;
					break;
				case ShellFacade.PHOTO_LOAD_COMPLETE:
					var i:uint = notification.getBody() as uint;
					slidePN.num = i + 1;
					if(cb.currentState == SlideCB.STATE_SINGLE_LOADING) {
						cb.currentState = SlideCB.STATE_PAUSED;
					}
			}
		}
		
		private function onPhotoChange(e:Event):void {
			cb.currentState = SlideCB.STATE_SINGLE_LOADING;
			sendNotification(ShellFacade.PHOTO_CHANGE, slidePN.num);
		}
		
		private function onPause(e:Event):void {
			cb.currentState = SlideCB.STATE_PAUSING;
			sendNotification(ShellFacade.PAUSE);
		}
		
		private function onContinue(e:Event):void {
			sendNotification(ShellFacade.CONTINUE);
		}
		
		private function get slidePN():PreviousAndNextHB {
			return cb.slidePN as PreviousAndNextHB;
		}
		
		private function get cb():SlideCB {
			return viewComponent as SlideCB;
		}
	}
}